﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace WebNhaXuatBan
{
    public partial class ThanhToan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["TaiKhoan"] != null)
            {
                TextBox1.Text = ((DTO.TaiKhoanDTO)Session["TaiKhoan"]).HoTen;
                TextBox2.Text = ((DTO.TaiKhoanDTO)Session["TaiKhoan"]).DiaChi;
                TextBox3.Text = ((DTO.TaiKhoanDTO)Session["TaiKhoan"]).DienThoai;
            }
            else
            {
                Response.Redirect("DangKy.aspx");
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList list = (ArrayList)Session["GioHang"];
            BUS.SachBUS ss=new BUS.SachBUS();
            float tongtien = 0;
            for (int i = 0; i < list.Count; i++)
            {
                tongtien += ((DTO.ChiTietGioHangDTO)list[i]).SoLuong * ss.LaySachTheoMaSach(((DTO.ChiTietGioHangDTO)list[i]).MaSach).GiaBan;
            }
            DTO.GioHangDTO gioHang = new DTO.GioHangDTO();
            gioHang.MaTaiKhoan = ((DTO.TaiKhoanDTO)Session["TaiKhoan"]).MaTaiKhoan;
            gioHang.NgayDatHang = DateTime.Now.ToShortDateString();
            gioHang.TongTien = tongtien;
            BUS.GioHangBUS gh = new BUS.GioHangBUS();
            int maGioHang=gh.ThemGioHang(gioHang);
            for (int i = 0; i < list.Count; i++)
            {
                DTO.SachDTO sachDTO = ss.LaySachTheoMaSach(((DTO.ChiTietGioHangDTO)list[i]).MaSach);
                if((sachDTO.SoLuongTon - ((DTO.ChiTietGioHangDTO)list[i]).SoLuong) >=  0)
                {
                    sachDTO.SoLuongTon=sachDTO.SoLuongTon-((DTO.ChiTietGioHangDTO)list[i]).SoLuong;
                    ((DTO.ChiTietGioHangDTO)list[i]).MaGioHang = maGioHang;
                    BUS.ChiTietGioHangBUS ct = new BUS.ChiTietGioHangBUS();
                    ct.ThemChiTietGioHang(((DTO.ChiTietGioHangDTO)list[i]));
                    ss.CapNhatSach(sachDTO);
                }
                else
                {
                    tongtien=tongtien-((DTO.ChiTietGioHangDTO)list[i]).SoLuong * ss.LaySachTheoMaSach(((DTO.ChiTietGioHangDTO)list[i]).MaSach).GiaBan;
                }
            }
            list = null;
            Session["GioHang"] = list;
            Response.Redirect("default.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}